package com.tinklabs.demo.activity;

import android.os.Bundle;

import com.tinklabs.demo.mvp.presenter.BasePresenter;

/**
 * Created by Jennifer on 2018/4/14.
 */
public abstract class PresenterActivity<P extends BasePresenter, V> extends BaseActivity {
    private P presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresenter();
    }

    /**
     * Must implement presenter instance
     */
    public void initPresenter() {
        presenter = createPresenter();
        if (presenter == null) {
            throw new NullPointerException("presenter空指针异常");
        }

        V view = createView();
        if (view == null) {
            throw new NullPointerException("view空指针异常");
        }

        presenter.attachView(view);
    }

    public abstract P createPresenter();

    public abstract V createView();

    public P getPresenter() {
        return presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

}
